package com.cby.android.server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @description: ServerService
 * @author: ChenBaoYang
 * @createDate: 2022/3/18 3:21 下午
 */
public class ServerService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startServer();
        Log.i("chenby","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }



    private void startServer()  {
        //ServerSocket创建TCP服务器端, 监听端口是8881
        ThreadPoolUtil.execute(new ServerListener());
    }



    static class ServerListener implements Runnable{

        private Socket socket;

        private boolean isRunning = false;

        @Override
        public void run() {
            try {
                isRunning = true;
                ServerSocket serverSocket = new ServerSocket(8888);
                //serverSocket.setSoTimeout(7000);
                //获取本地的网络地址数据
                InetAddress address = InetAddress.getLocalHost();
                //获取本地ip
                String ip = address.getHostAddress();
                Log.i("chenby","服务端IP地址："+ip);
                //调用accept()方法监听客户端的请求
                while(isRunning){
                    Log.i("chenby","accept：before");
                    socket = serverSocket.accept();
                    Log.i("chenby","accept：after");
                     //executor.execute(new SeverSend(socket));
                    InputStream is = socket.getInputStream();
                    ObjectInputStream ois = new ObjectInputStream(is);
                    //获取数据转成字符串
                    String req = (String) ois.readObject();
                    Log.i("chenby","客户端发送过来的信息:" + req);
                    OutputStream os = socket.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os);
                    String res = "客户端返回"+ip;
                    oos.writeObject(res);
                    oos.flush();
                    oos.close();
                }


            } catch (IOException e) {
                Log.i("chenby", "error = "+e.getMessage());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        public void stop() {
            isRunning = false;
        }
    }

    static class SeverSend implements Runnable{

        private final Socket socket;

        public SeverSend(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);
                // 获取客户端的ip地址
                InetAddress inetAddress = InetAddress.getLocalHost();
                String ip = inetAddress.getHostAddress();
                while(true){
                    Thread.sleep(5000L);
                    // 输出内容
                    pw.write("客户端：~" + ip + "~ 接入服务器！！");
                    pw.flush();
                    pw.close();
                }

            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
